<template>
  <d2-container>
    <template slot="header">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="方案名称">
          <el-input v-model="searchForm.planName" placeholder="请输入方案名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="启用" value="active"></el-option>
            <el-option label="停用" value="inactive"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="handleAdd">新增方案</el-button>
        </el-form-item>
      </el-form>
    </template>
    
    <el-table
      :data="tableData"
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        prop="planName"
        label="方案名称"
        width="150">
      </el-table-column>
      <el-table-column
        prop="electricPrice"
        label="电费单价"
        width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.electricPrice }}/kWh
        </template>
      </el-table-column>
      <el-table-column
        prop="waterPrice"
        label="水费单价"
        width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.waterPrice }}/m³
        </template>
      </el-table-column>
      <el-table-column
        prop="peakPrice"
        label="峰时电价"
        width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.peakPrice }}/kWh
        </template>
      </el-table-column>
      <el-table-column
        prop="valleyPrice"
        label="谷时电价"
        width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.valleyPrice }}/kWh
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
            {{ scope.row.status === 'active' ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button 
            size="mini" 
            :type="scope.row.status === 'active' ? 'warning' : 'success'"
            @click="handleToggleStatus(scope.row)">
            {{ scope.row.status === 'active' ? '停用' : '启用' }}
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <template slot="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </template>
  </d2-container>
</template>

<script>
import faker from 'faker'

export default {
  name: 'SystemChargePlan',
  data () {
    return {
      loading: false,
      searchForm: {
        planName: '',
        status: ''
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      // 模拟数据
      const data = {
        totalCount: 30,
        pageSize: 10,
        totalPage: 3,
        currPage: this.pagination.currentPage,
        list: Array.from({ length: 10 }, (_, index) => ({
          id: index + 1,
          planName: `收费方案${index + 1}`,
          electricPrice: faker.finance.amount(0.5, 1.5, 2),
          waterPrice: faker.finance.amount(2, 5, 2),
          peakPrice: faker.finance.amount(0.8, 2, 2),
          valleyPrice: faker.finance.amount(0.3, 0.8, 2),
          status: faker.random.boolean() ? 'active' : 'inactive',
          createTime: faker.date.past().toISOString().replace('T', ' ').split('.')[0]
        }))
      }
      this.tableData = data.list
      this.pagination.total = data.totalCount
      this.loading = false
    },
    handleSearch () {
      this.pagination.currentPage = 1
      this.getList()
    },
    handleReset () {
      this.searchForm = {
        planName: '',
        status: ''
      }
      this.pagination.currentPage = 1
      this.getList()
    },
    handleAdd () {
      this.$message.info('新增收费方案功能开发中...')
    },
    handleEdit (row) {
      this.$message.info(`编辑收费方案: ${row.planName}`)
    },
    handleToggleStatus (row) {
      const action = row.status === 'active' ? '停用' : '启用'
      this.$confirm(`确认${action}收费方案 ${row.planName}？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success(`${action}成功`)
        this.getList()
      }).catch(() => {
        this.$message.info(`已取消${action}`)
      })
    },
    handleDelete (row) {
      this.$confirm(`确认删除收费方案 ${row.planName}？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    handleSizeChange (val) {
      this.pagination.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.demo-form-inline .el-form-item {
  margin-bottom: 10px;
}
</style>

